/**
	Copyright (c) 2010 ATTi 
	All rights reserved.
	Author: Michael Shu
	Date: 10/27/2010
*/

global class AASOpportunityMonthlyUpdate implements Schedulable{
	/* To schedule it at 1:00am 1st day of every month , in Anonymous Blocks and execute below commands
	   AASOpportunityMonthlyUpdate a0 = new AASOpportunityMonthlyUpdate();
	   String sch0 = AASOpportunityMonthlyUpdate.CRON_EXP0;
	   System.schedule('Schedule AAS Opportunity update Monthly', sch0, a0);	
	*/
	public static String CRON_EXP0  = '0 00 1 1 * ?';
	
	global void execute (SchedulableContext SC){
		Date topDate = Date.today().toStartOfMonth();
		Date bottomDate = Date.today().addYears(-1).toStartOfMonth();
		List<Opportunity> opps = [select ID from Opportunity 
		                          where RecordType.Name = 'Advanced Ad Solutions' and (((CloseDate <:  bottomDate or CloseDate >=: topDate)and AAS_AGE_LESS_THAN_12_MONTHS__C =: true) 
		                                                                            or ( CloseDate >=: bottomDate and CloseDate <: topDate and Probability >=: 90 and AAS_AGE_LESS_THAN_12_MONTHS__C =: false)) ];
		try{
			update opps; // OppForNatSalesHelperClass.setAASOppfields(opps) will set AAS_AGE_LESS_THAN_12_MONTHS__C to true or false
		}catch(Exception ex){
			GeneralConstants.mailAnyExceptionToMe('AASOpportunityMonthlyUpdate', 'AAS monthly update fail', ex);
		}
	}
}